<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/jquery-impromptu.min.css">
    <link rel="stylesheet" href="css/jquery-ui.css">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="dataLoad" style="display:none; position: fixed; width: 100%; top: 20%; z-index: 1001;">
           <table width=100% height=100% border=0 align=center valign=middle>
            <tr height=50%><td align=center>&nbsp;</td></tr>
            <tr><td align=center><img src="images/loading.gif"/></td></tr>
            <tr><td align=center id="loading_text">获取中，请稍后......</td></tr>
            <tr height=50%><td align=center>&nbsp;</td></tr>
           </table>
         </div>
    <div id="search_bar" class="mt10">
        <div class="box">
            <div class="box_border">
                <div class="box_top"><b class="pl15">数据筛选</b></div>
                <div class="box_center pt10 pb10" style="padding-left: 45%;">
                    <table class="form_table" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td>当前状态</td>
                            <td>
                                 <span class="fl">
                                     <div class="select_border">
                                         <div class="select_containers ">
                                             <select name="" class="select" id="approve_state">
                                                 <option value="0">待审批</option>
                                                 <option value="1">已同意</option>
                                                 <option value="2">已驳回</option>
                                             </select>
                                         </div>
                                     </div>
                                 </span>
                            </td>
                            <td><input type="button" value="确定" id="confirm_query" class="ext_btn ext_btn_submit"></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="mt10">
        <div class="box">
            <div class="box_border">
                <div class="box_top"><b class="pl15">报销费用信息</b></div>
                <div class="mt10">
                    <div id="table" class="mt10">
                        <div class="box span10 oh" style="margin-left: auto; margin-right: auto">
                            <table id="ordinary_fee" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.2rem;">
                                <tr>
                                    <th class="td_center" style="width:10%">编号</th>
                                    <th class="td_center" style="width:15%">申请人</th>
                                    <th class="td_center" style="width:15%">所属部门</th>
                                    <th class="td_center" style="width:15%">预借金额</th>
                                    <th class="td_center" style="width:15%">申请时间</th>
                                    <th class="td_center" style="width:15%">当前状态</th>
                                    <th class="td_center" style="width:15%">可选操作</th>
                                </tr>
                            </table>
                            <div class="page mt10">
                                <div class="pagination">
                                    <ul>
                                        <li class="first-child" ><a href="#" id="firstPage">首页</a></li>
                                        <li class="" ><a href="#" id="prevPage">上一页</a></li>
                                        <li class="" ><a href="#" id="nextPage">下一页</a></li>
                                        <li class="last-child " ><a href="#" id="endPage">末页</a></li>
                                        <li class="disabled pagedata" id="page1"><a id="pageInfo">1/20</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/config.js"></script>
<script type="text/javascript" src="js/jquery-impromptu.min.js"></script>
<script>
    var firstSet = true;
    var curFeeData = {};
    //辅助函数，获取当前用户所有的借款信息
    function getUserOrdinaryLoanFee(){
        $('#dataLoad').show();
        var userinfo = JSON.parse(window.localStorage.getItem('user_info'));
        var state = $('#approve_state').val();
        $.post(url + 'LoanFee/GetMyOrdinaryLoanFee.php', {'user_id':userinfo.user_id, 'state':state}, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1){
                curFeeData = t;
                if(firstSet){
                    createTablePage(t.totalCount);
                    firstSet = false;
                }


                initLoanFeeTable();

            }
            else{
                createTablePage(0);
                $.prompt("<h3>网络或数据库故障，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }

    //辅助函数，显示表格数据
    function initLoanFeeTable(){
        //清空当前数据
        $('.info_tr').empty();
        if(curFeeData.totalCount == 0){
            return;
        }

        var start = (curPage-1) * rowPerPage;
        var end = (curFeeData.data.length - start)> rowPerPage ? rowPerPage : curFeeData.data.length;
        for(var i = start; i < end; i++) {
            $('#ordinary_fee').append(
                    "<tr class='tr info_tr'>" +
                        "<td class='td_center'>" + ((i + 1) + (curPage - 1) * rowPerPage) + "</td>" +
                        "<td class='td_center'>" + curFeeData.data[i].user_name + "</td>" +
                        "<td class='td_center'>" + curFeeData.data[i].section_name + "</td>" +
                        "<td class='td_center'>" + curFeeData.data[i].loan_money + "</td>" +
                        "<td class='td_center'>" + curFeeData.data[i].app_time + "</td>" +
                        "<td class='td_center'>" + getLoanApproveState(curFeeData.data[i].state, curFeeData.data[i].state_detail) + "</td>" +
                        "<td class='td_center'>" +
                            "<a class='ext_btn browse browse_btn'><span class='browse'></span>查看</a>" +
                            "<a class='ext_btn print print_btn'><span class='print'></span>打印</a>" +
                        "</td>" +
                    "</tr>");
            if(curFeeData.data[i].state == 2 || curFeeData.data[i].state == 0){
                $(".print").eq(i*2).addClass('disabled');
                $(".print").eq(i*2+1).addClass('disabled');
            }
        }
        $('.browse_btn').each(function(index, elem){
            $(this).click(function(){
                var id = $(this).parent().parent().children('td').eq(0).html();
               processLoanFeeApprove(id);

            })
        });
        $('.print_btn').each(function(index, elem){
            $(this).click(function(){
                var id = $(this).parent().parent().children('td').eq(0).html();
                printApprovePDF(id);
            });
        });

    }

    //辅助函数，用于处理借款单打印
    function printApprovePDF(id){
        $('#dataLoad').show();
        $.post(url + 'PDF/CreateLoanFeePDF.php',
            {'appID': curFeeData.data[id-1].app_id, 'byApp':0}, function(data){
            var t= JSON.parse(data);
            $('#dataLoad').hide();
            if(t.state == 1){
                $.prompt("<h3>生成成功</h3>", {
                    title: '操作提醒',
                    buttons: {'确定': true}
                });
                window.open(url + t.url);
            }
            else{
                $.prompt("<h3>网络或数据库错误，请联系管理员</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                })
            }
        });
    }

    //辅助函数，用于处理借款审批
    function processLoanFeeApprove(id){
        $('#dataLoad').show();
        $.post(url + 'LoanFee/GetOrdinaryLoanFeeByID.php', {'appID' : curFeeData.data[id-1].app_id}, function(data){
            $('#dataLoad').hide();

            var t = JSON.parse(data);
            if(t.state == 1){
                window.localStorage.setItem('curItem', data);
                window.localStorage.setItem('approve_state', 0);
                jumpPage('loan_fee_info.html');
            }
            else{
                $.prompt("<h3>网络或数据库故障，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true},
                })
            }
        })
    }

    $(function(){
        getUserOrdinaryLoanFee();
        $('#confirm_query').click(function(){
            getUserOrdinaryLoanFee();
        });

        clickFirstPage(initLoanFeeTable);
        clickEndPage(initLoanFeeTable);
        clickNextPage(initLoanFeeTable);
        clickPrevPage(initLoanFeeTable);
    });

</script>